package zabbix

import (
	"encoding/json"
	"errors"
)

func TemplateCreateData() (string,map[string]interface{}) {
	method := "template.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "host": "Linux template",
        "groups": {
            "groupid": 1
        },
        "hosts": [
            {
                "hostid": "10084"
            },
            {
                "hostid": "10090"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateDeleteData() (string,map[string]interface{}) {
	method := "template.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"13",
		"32"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateMassremoveData() (string,map[string]interface{}) {
	method := "template.massremove"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "templateids": [
            "10085",
            "10086"
        ],
        "groupids": "2"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateMassupdateData() (string,map[string]interface{}) {
	method := "template.massupdate"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "templates": [
            {
                "templateid": "10085"
            },
            {
                "templateid": "10086"
            }
        ],
        "templates_clear": [
            {
                "templateid": "10091"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateMassaddData() (string,map[string]interface{}) {
	method := "template.massadd"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "templates": [
            {
                "templateid": "10085"
            },
            {
                "templateid": "10086"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateUpdateData() (string,map[string]interface{}) {
	method := "template.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "templateid": "10086",
        "name": "Template OS Linux"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TemplateGetData() (string,map[string]interface{}) {
	method := "template.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "filter": {
            "host": [
                "Template OS Linux",
                "Template OS Windows"
            ]
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}



func TemplateCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateMassremove(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateMassremoveData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateMassupdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateMassupdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateMassadd(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateMassaddData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func TemplateGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TemplateGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



